import axios from 'axios'
import { getToken ,removeToken} from '../uitls/auth'
const instance = axios.create({
    baseURL: 'http://localhost:5000/',
    timeout: 3000,
})

//对每一次请求进行拦截，逻辑操作
instance.interceptors.request.use((config) => {
    let token = getToken();
    //如果登录则授权
    if (token) {
        config.headers['Authorization'] = 'Bearer ' + token
    }
    return config
}, (err) => {
    return Promise.reject(err)
}
)

//对每次的响应进行拦截，逻辑操作
instance.interceptors.response.use((response) => {
    if (response.status === 200) {
        
        return response
    } else if (response.status === 415) {
        //如果token没通过
        removeToken()
    }

    return response

}, (err) => {
    return Promise.reject(err)
}
)
export default instance